<?php
/**
 * Ohlasky View for Ohlasky Component
 * @license		GNU/GPL
 */

jimport( 'joomla.application.component.view');

/**
 * HTML View class for the Ohlasky Component
 */
class OhlaskyViewOhlasky extends JView {
    function display($tpl = null) {

        $tydenni_ohlasky_arr = $this->get('Aktualni_ohlasky');
        //$aktualni_ohlasky = $this->getAktualni_ohlasky;
        //$this->assignRef( 'aktualni_ohlasky', $aktualni_ohlasky);
        //parent::display($tpl);

        //uprava mime typu
        $document =& JFactory::getDocument();
        $document->setMimeEncoding('application/rtf');

        //priprava dat
        if (is_array($tydenni_ohlasky_arr)) {
            if (empty($tydenni_ohlasky_arr['chyba'])) {
                $bunky_arr = $tydenni_ohlasky_arr['bunky_arr'];
                $dny_tydne_arr = $tydenni_ohlasky_arr['dny_tydne_arr'];
                $mista_arr = $tydenni_ohlasky_arr['mista_arr'];
                $nalezeny_tyden_obj = $tydenni_ohlasky_arr['nalezeny_tyden_obj'];
                $odstavce_obj_list = $tydenni_ohlasky_arr['odstavce_obj_list'];
                $vc_predch_nedele = $tydenni_ohlasky_arr['vc_predch_nedele'];
                $predch_tyden_obj = $tydenni_ohlasky_arr['predch_tyden_obj'];
                $nastaveni = $tydenni_ohlasky_arr['nastaveni'];

                if (is_object($nalezeny_tyden_obj)) {
                    $ode_dne = $nalezeny_tyden_obj->ode_dne;
                    //zjistime pocet radku tabulky
                    $pocet_radku = count($dny_tydne_arr) + 1;

                    //zjistime pocet sloupcu a sirku sloupce
                    $pocet_sloupcu = count($mista_arr) + 1;
                    $sirka_sloupce = 1;

                    //stanovime sirku sloupce (dle poctu sloupcu)
                    switch ($pocet_sloupcu) {
                        case 2:
                            $sirka_sloupce = 6;
                            break;
                        case 3:
                            $sirka_sloupce = 4;
                            break;
                        case 4:
                            $sirka_sloupce = 3;
                            break;
                        case 5:
                            $sirka_sloupce = 3;
                            break;
                        case 6:
                            $sirka_sloupce = 2;
                            break;
                        case 7:
                            $sirka_sloupce = 2;
                            break;
                    }

                    $nadpis = 'BOHOSL';
                    if (is_object($nastaveni)) {
                        $nadpis = $nastaveni->nadpis_tabulky;
                    }

                    require_once(JPATH_COMPONENT.DS.'lib'.DS.'phprtflite'.DS.'rtf'.DS.'Rtf.php');

                    $times8 = new Font(9, 'Times new Roman');
                    $times12 = new Font(13, 'Times new Roman');
                    $arial14 = new Font(14, 'Arial', '#000066');

                    $parFormat = new ParFormat();

                    //rtf document
                    $rtf = new Rtf();
                    //borders
                    //$rtf->setBorders(new BorderFormat(2, '#000fff'), true, true, true, true);
                    //$rtf->setBorders(new BorderFormat(2, '#ff0000'), 0, 1, 0, 1);
                    //headers
                    $rtf->setOddEvenDifferent(0);

                    $ode_dne_str = '';
                    $ode_dne_obj = date_create($nalezeny_tyden_obj->ode_dne);
                    if (is_object($ode_dne_obj)) {
                        $ode_dne_str = $ode_dne_obj->format('j.n.Y');
                    }
                    $rok_tydne_str = strval($nalezeny_tyden_obj->rok);


                    $header = &$rtf->addHeader('right');
                    $text_hlavicky = 'Ohlášky od '.$ode_dne_str;
                    $header->writeText($text_hlavicky, $times8, $parFormat);



                    //section 1
                    $sect = &$rtf->addSection();
                    //$sect->setPaperHeight(16);
                    //$sect->setPaperWidth(25);

                    //Borders overriden: No Borders
                    $sect->setBorders(new BorderFormat(0));
                    //$sect->setSpaceBetweenColumns(1);
                    //$sect->setColumnsCount(1);
                    //$sect->setLineBetweenColumns();

                    //writeSectionText($sect, $arial14, $times12, $text, $text2, $text3);

                    //Fonts
                    $fontHead = new Font(12, 'Arial');
                    $fontSmall = new Font(3);
                    $fontAnimated = new Font(10);
                    $fontLink = new Font(10, 'Helvetica', '#0000cc');

                    $parBlack = new ParFormat();
                    $parBlack->setIndentRight(9);
                    $parBlack->setBackColor('#000000');
                    $parBlack->setSpaceBefore(12);

                    $parHead = new ParFormat('center');
                    $parHead->setSpaceBefore(3);
                    $parHead->setSpaceAfter(3);
                    $parHead->setIndentLeft(0.2);
                    $parHead->setIndentRight(0.2);

                    $parHeadLeft = new ParFormat('left');
                    $parHeadLeft->setSpaceBefore(3);
                    $parHeadLeft->setSpaceAfter(1);
                    $parHeadLeft->setIndentLeft(0.2);
                    $parHeadLeft->setIndentRight(0.2);

                    $parHeadLeft2 = new ParFormat('left');
                    $parHeadLeft2->setSpaceAfter(3);
                    $parHeadLeft2->setIndentLeft(0.2);
                    $parHeadLeft2->setIndentRight(0.2);

                    $parHeadOdst = new ParFormat('left');
                    $parHeadOdst->setSpaceBefore(6);
                    $parHeadOdst->setSpaceAfter(2);
                    $parHeadOdst->setIndentLeft(1);



                    $parSimple = new ParFormat();
                    $parSimple->setSpaceBefore(3);
                    $parSimple->setIndentFirst(1);

                    $parPhp = new ParFormat();
                    $parPhp->setShading(5);
                    $parPhp->setBorders(new BorderFormat(1, '#000000', 'dash', 0.3));
                    $parPhp->setIndentLeft(5);
                    $parPhp->setIndentRight(0.5);

                    //section
                    //$sect = &$rtf->addSection();


                    $table = &$sect->addTable();

                    //vytvorime bunky tabulky
                    //parametr je pocet radku
                    $table->addRows($pocet_radku);
                    for ($j = 1; $j <= $pocet_sloupcu; $j++) {
                    //parametr je sirka sloupce
                        $table->addColumn($sirka_sloupce);
                    }
                    $borderFormat = new BorderFormat(1, $color = '#000000', $space = 4);
                    $table->setFirstRowAsHeader();
                    $table->setBordersOfCells(&$borderFormat, 1, 1, $pocet_radku, $pocet_sloupcu);
                    $table->setDefaultAlignmentOfCells($alignment = 'center', 1, 2, $pocet_radku, $pocet_sloupcu);
                    //$table->setDefaultAlignmentOfCells($alignment, $startRow, $startColumn, $endRow, $endColumn)

                    //do bunek tabulky budeme vepisovat hodnoty
                    //zahlavi tabulky:
                    $cell = &$table->getCell(1, 1);
                    $cell->writeText($nadpis, new Font(10, 'Arial'), $parHead);
                    $sloupec_zahlavi = 2;
                    foreach ($mista_arr as $misto) {
                        $cell = &$table->getCell(1, $sloupec_zahlavi);
                        $cell->writeText($misto, new Font(12, 'Arial'), $parHead);
                        $sloupec_zahlavi++;
                    }

                    $radek = 2;
                    foreach ($dny_tydne_arr as $den_tydne) {
                        $nadpis_dne = '';
                        switch ($den_tydne) {
                            case '-1':
                                $date = new DateTime($ode_dne);
                                $date->modify('-1 day');
                                $datum_str = $date->format('j. n. Y');
                                $horni_radek = 'NEDĚLE  ';
                                $dolni_radek = '';
                                if (is_object($predch_tyden_obj)) {
                                    $dolni_radek = $predch_tyden_obj->litur_ne;
                                }
                                break;
                            case '1':
                                $date = new DateTime($ode_dne);
                                $datum_str = $date->format('j. n. Y');
                                $horni_radek = 'PONDĚLÍ  ';
                                $dolni_radek = $nalezeny_tyden_obj->litur_po;
                                break;
                            case '2':
                                $datum_ut_obj = new DateTime($ode_dne);
                                $datum_ut_obj->modify("+1 day");
                                $datum_str = $datum_ut_obj->format('j. n. Y');
                                $horni_radek = 'ÚTERÝ  ';
                                $dolni_radek = $nalezeny_tyden_obj->litur_ut;
                                break;
                            case '3':
                                $datum_st_obj = new DateTime($ode_dne);
                                $datum_st_obj->modify("+2 day");
                                $datum_str = $datum_st_obj->format('j. n. Y');
                                $horni_radek = 'STŘEDA  ';
                                $dolni_radek = $nalezeny_tyden_obj->litur_st;
                                break;
                            case '4':
                                $datum_ct_obj = new DateTime($ode_dne);
                                $datum_ct_obj->modify("+3 day");
                                $datum_str = $datum_ct_obj->format('j. n. Y');
                                $horni_radek = 'ČTVRTEK  ';
                                $dolni_radek = $nalezeny_tyden_obj->litur_ct;
                                break;
                            case '5':
                                $datum_pa_obj = new DateTime($ode_dne);
                                $datum_pa_obj->modify("+4 day");
                                $datum_str = $datum_pa_obj->format('j. n. Y');
                                $horni_radek = 'PÁTEK  ';
                                $dolni_radek = $nalezeny_tyden_obj->litur_pa;
                                break;
                            case '6':
                                $datum_so_obj = new DateTime($ode_dne);
                                $datum_so_obj->modify("+5 day");
                                $datum_str = $datum_so_obj->format('j. n. Y');
                                $horni_radek = 'SOBOTA  ';
                                $dolni_radek = $nalezeny_tyden_obj->litur_so;
                                break;
                            case '7':
                                $datum_ne_obj = new DateTime($ode_dne);
                                $datum_ne_obj->modify("+6 day");
                                $datum_str = $datum_ne_obj->format('j. n. Y');
                                $horni_radek = 'NEDĚLE  ';
                                $dolni_radek = $nalezeny_tyden_obj->litur_ne;
                                break;
                        }
                        //vypiseme levou krajni bunku s nadpisem dne
                        $cell = &$table->getCell($radek, 1);
                        $horni_radek = '<b>'.$horni_radek.'</b>';
                        $druhy_radek = '<b>'.$datum_str.'</b>';
                        $cell->writeText($horni_radek, new Font(10, 'Arial'), $parHeadLeft);
                        $cell->writeText($druhy_radek, new Font(10, 'Arial'), $parHeadLeft2);
                        $cell->writeText($dolni_radek, new Font(9, 'Arial'), $parHeadLeft);

                        //for ($j = 1; $j <= 4; $j++)
                        $sloupec = 2;
                        foreach ($mista_arr as $misto) {
                            $bunka_arr = $bunky_arr[$den_tydne][$misto];
                            $text_bunky = '';
                            foreach($bunka_arr as $udalost) {
                                $cell = &$table->getCell($radek, $sloupec);
                                if (is_object($udalost)) {
                                    $text_udalosti = '';
                                    $datum = $udalost->datum;
                                    $cas_old = $udalost->cas;
                                    $cas_konec = '';
                                    $cas_konec_bool_int = strpos($udalost->cas_konec, '00:00');
                                    if (($cas_konec_bool_int === false) || ($cas_konec_bool_int !== 0)) {
                                        $date_time_obj2 = date_create($datum.' '.$udalost->cas_konec);
                                        if (is_object($date_time_obj2)) {
                                            $cas_konec = $date_time_obj2->format('G:i');
                                        }
                                    }
                                    $date_time_obj = new DateTime($datum.' '.$cas_old);
                                    $cas_udalosti = $date_time_obj->format('G:i');
                                    if (!empty($cas_konec)) {
                                        $cas_udalosti =  $cas_udalosti.' - '.$cas_konec;
                                    }
                                    $text_udalosti = $udalost->hruby_popis;
                                    //$cell = &$table->getCell($radek, $sloupec);
                                    $cell->writeText($cas_udalosti, new Font(14, 'Arial'), $parHead);
                                    $cell->writeText($text_udalosti, new Font(10, 'Arial'), $parHead);
                                    $cell->writeText('', new Font, new ParFormat());
                                } else {
                                    $cell->writeText('', new Font, new ParFormat());
                                }
                            }
                            //$i a $j musi byt integer
                            //                $cell = &$table->getCell($i, $j);
                            //                $cell->writeText('18:30', new Font(14, 'Arial'), $parHead);
                            //                $cell->writeText('po mši sv. svačina na farní zahradě', new Font(10, 'Arial'), $parHead);
                            //$par->setSpaceBetweenLines(2);

                            $sloupec++;
                        }
                        $radek++;
                    }

                    if (is_array($odstavce_obj_list)) {
                        foreach($odstavce_obj_list as $odstavec_obj) {
                            if (is_object($odstavec_obj)) {
                            //$sect = &$rtf->addSection();
                                if (!empty($odstavec_obj->nadpis)) {
                                    $nadpis_odstavce = '<b>'.$odstavec_obj->nadpis.'</b>';
                                    $sect->writeText($nadpis_odstavce, new Font(10, 'Arial'), $parHeadOdst);
                                //echo '<h3>'.$odstavec_obj->nadpis.'</h3>';
                                }
                                //echo '<p>'.$odstavec_obj->obsah.'</p>';
                                $sect->writeText($odstavec_obj->obsah, new Font(10, 'Arial'), $parSimple);
                            }
                        }
                    }
                    $jmeno_souboru = 'ohlasky_'.$ode_dne_str;

                    $rtf->prepare();
                    $rtf->sendRtf($jmeno_souboru);


                //$rtf ->save(JPATH_COMPONENT.DS.'dokumenty'.DS.$jmeno_souboru);
                //chmod(JPATH_COMPONENT.DS.'dokumenty', 0777);
                //chmod(JPATH_COMPONENT.DS.'dokumenty'.DS.$jmeno_souboru, 0777);

                }
            }
        }


    //vystup dat v RTF formatu

    }
}

